#!/usr/bin/env bash

maglev_install()
{
  __rvm_ensure_has_mri_ruby
  compatible_ruby="$(__rvm_mri_ruby)"

  rvm_log "Running MagLev prereqs checking script."

  "$rvm_scripts_path/maglev" ||
  {
    result=$?
    rvm_error "Prerequisite checks have failed. \nHalting the installation."
    return $result
  }

  __rvm_cd "${rvm_src_path}"

  if
    [[ ! -d "${rvm_src_path}/$rvm_ruby_string" ]] ||
    (( ${rvm_head_flag:=0} == 1 ))
  then
    __rvm_fetch_ruby ||
    {
      result=$?
      rvm_error "There has been an error while trying to fetch the source.  \nHalting the installation."
      exit $result
    }
  fi

  system="${_system_type}"
  arch="${_system_arch}"
  if [[ "${system}-${arch}" == "Darwin-x86_64" ]]
  then arch="i386"
  fi

  if
    (( ${rvm_head_flag:=0} == 1 ))
  then
    __rvm_cd "${rvm_src_path}/$rvm_ruby_string"

    rvm_gemstone_package_file="GemStone-$(__rvm_grep "^GEMSTONE" version.txt | cut -f2 -d-).${system}-${arch}"

    rvm_gemstone_url="$maglev_url/${rvm_gemstone_package_file}.${rvm_archive_extension}"
  fi

  rvm_log "Downloading the GemStone package, this may take a while depending on your connection..."

  "$rvm_scripts_path/fetch" "$rvm_gemstone_url" ||
  {
    result=$?
    rvm_error "There has been an error while trying to fetch the GemStone package.\nHalting the installation."
    return $result
  }

  __rvm_cd "${rvm_src_path}"

  if [[ -s "$rvm_ruby_package_file" ]]
  then \command \mv "$rvm_ruby_package_file" "${rvm_src_path}/$rvm_ruby_string"
  fi

  __rvm_cd "${rvm_src_path}/$rvm_ruby_string"

  if
    [[ -d "${rvm_src_path}/${rvm_gemstone_package_file}" ]]
  then
    __rvm_log_command "gemstone.fix_rights" \
      "Fixing gemstone rights" \
      chmod -R u+w "${rvm_src_path}/${rvm_gemstone_package_file}"
  else
    mkdir -p "${rvm_src_path}/${rvm_gemstone_package_file}"
  fi

  __rvm_log_command "extract" \
    "$rvm_ruby_string - #extracting $rvm_gemstone_package_file to ${rvm_src_path}" \
    __rvm_package_extract "${rvm_archives_path}/$rvm_gemstone_package_file.$rvm_archive_extension" "${rvm_src_path}" ||
  case $? in
    199)
      rvm_error "\nUnrecognized archive format '$archive_format'"
      return 199
      ;;
    *)
      rvm_error "There has been an error while trying to extract the source. Halting the installation."
      return 1
      ;;
  esac

  ln -fs "${rvm_src_path}/$rvm_gemstone_package_file" "gemstone"

  __rvm_rm_rf $rvm_ruby_home

  __rvm_log_command "install" \
    "Installing maglev to $rvm_ruby_home" \
    __rvm_cp -Rf "${rvm_src_path}/$rvm_ruby_string" "$rvm_ruby_home"

  (
    __rvm_cd "$rvm_ruby_home/bin/"

    for binary in maglev-irb maglev-ruby maglev-gem
    do
      ln -fs "$binary" "${binary#maglev-}"
    done
    unset binary
  )

  __rvm_cd "$rvm_ruby_home"

  if
    (( ${rvm_head_flag:=0} == 1 ))
  then
    \command \git submodule update --init
    "$rvm_ruby_home/bin/maglev" force-reload
  fi

  ln -fs maglev.demo.key-${system}-${arch} etc/maglev.demo.key

  __rvm_log_command "build:maglev" \
    "Bootstrapping a new image" \
    "$rvm_wrappers_path/$compatible_ruby/rake" "build:maglev"

  [[ -e ${rvm_ruby_home}/etc/conf.d/maglev.conf ]] ||
  __rvm_log_command "stone.create" \
    "Creating default 'maglev' repository." \
    "$rvm_wrappers_path/$compatible_ruby/rake" "stone:create[maglev]" >/dev/null 2>&1

  __rvm_log_command "rdoc" \
    "Generating maglev HTML documentation" \
    "$rvm_wrappers_path/$compatible_ruby/rake" rdoc >/dev/null 2>&1

  __rvm_log_command "stwrappers" \
    "Generating smalltalk FFI." \
    "$rvm_wrappers_path/$compatible_ruby/rake" stwrappers >/dev/null 2>&1

  unset compatible_ruby

  __rvm_initial_gemsets_create_without_rubygems "$rvm_ruby_home/bin/ruby" &&
  __rvm_irbrc &&
  __rvm_fetch_ruby_cleanup
}
